﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace GameLogic.Objects
{
    abstract class GameObject
    {
        private int hitPoints;

        public Rectangle Position { get; set; }
        public int Width
        {
            get
            {
                return Position.Width;
            }
        }
        public int Height
        {
            get
            {
                return Position.Height;
            }
        }

        //public static readonly EmptyCell Empty = new EmptyCell();
        public abstract void Draw(Graphics graphics);
        public void DrawByPicture(Graphics graphics, Image image, Rectangle place)
        {
            graphics.DrawImageUnscaled(image, place);
        }

        public abstract Image GetCurrentImage();
        public virtual int HitPoints
        {
            get
            {
                return hitPoints;
            }
            set
            {
                hitPoints = value;
            }
        }

        public override bool Equals(object obj)
        {
            var another = obj as GameObject;
            if (another == null) return false;
            return
                Width == another.Width &&
                Height == another.Height &&
                Position.Equals(another.Position) &&
                HitPoints == another.HitPoints;
        }

        public override int GetHashCode()
        {
            return Width ^ Height;
        }

        public bool IntersectsWithSmth(Rectangle newPos, Map map)
        {
            var gameObjects = map.Objects;
            foreach (var gameObject in gameObjects)
            {
                if (gameObject != this && newPos.IntersectsWith(gameObject.GetPhysRectangle()))
                {
                    return true;
                }
            }

            //сравнить с появившимися
            var born = map.AliveObjects.Except(gameObjects);
            foreach (var gameObject in born)
            {
                if (gameObject != this && newPos.IntersectsWith(gameObject.GetPhysRectangle()))
                {
                    return true;
                }
            }

            return false;
        }

        public virtual Rectangle GetPhysRectangle()
        {
            return Position;
        }
    }
}
